﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections.ObjectModel;
using WindowsGame1.SpriteClasses;


namespace WindowsGame1.DialogClasses
{
    public class Script
    {
        Dictionary<string, Dialog> dialogs;

        public Script(params Dialog[] dialogs)
        {
            this.dialogs = new Dictionary<string, Dialog>();
            foreach (Dialog dialog in dialogs)
            {
                this.dialogs.Add(dialog.Name, dialog);
            }
        }

        public Dialog this[string name]
        {
            get { return dialogs[name]; ; }
        }
    }



    public class Dialog
    {
        public Collection<DialogHandler> handlers;
        string name;
        string text;
        public Dialog(string name, string text,
        params DialogHandler[] handlers)
        {
            this.handlers = new Collection<DialogHandler>();
            this.name = name;
            this.text = text;
            foreach (DialogHandler handler in handlers)
                this.handlers.Add(handler);
        }
    
        public int HandlerCount
        {
            get { return handlers.Count; }
        }
        
        public string Name
        {
            get { return name; }
        }
        
        public string Text
        {
            get { return text; }
        }
        
        public void InvokeHandler(NPC npc, int currentHandler)
        {
            handlers[currentHandler].Invoke(npc);
        }
    }



    public class DialogHandler
    {
        string caption;
        DialogAction[] actions;
        
        public DialogHandler(string caption, params DialogAction[] actions)
        {
            this.caption = caption;
            this.actions = actions;
        }
    
        public string Caption
        {
            get { return caption; }
        }

        public void Invoke(NPC npc)
        {
            foreach (DialogAction action in actions)
                action.Invoke(npc);
        }
    }

    
    
    public class DialogAction
    {
        MethodInfo method;
        object[] parameters;
    
        public DialogAction(string methodName, object[] parameters)
        {
            method = typeof(NPC).GetMethod(methodName);
            this.parameters = parameters;
        }
        
        public void Invoke(NPC npc)
        {
            method.Invoke(npc, parameters);
        }
    }
}